
import React from "react";
import {Form,Input} from "antd";
import {diffProps} from "../../libs/funcs";
import { observer } from 'mobx-react';
/**
 * 输入框
 */
@observer
class FormInput extends React.Component{

    constructor(props) {
        super(props);
        this.state={};
        this.store = props.store;
    }

    handleInputChange = (event) => {
        const { name, value } = event.target;
        if(this.store){
            this.store.updateField(name, value);
        }
    };

    render() {
        const {getFieldDecorator} =  this.props.form;
        const {initialValue,label,field,rules,allowClear,placeholder,disabled,addonAfter,addonBefore,form_my_xuan_jian_store,form_store,readonly} = this.props;
        const new_props = diffProps(this.props,["form","initialValue","label","field","rules","allowClear","placeholder"]);
        let addonBefore_new = addonBefore;
        let that = this;
        if(form_my_xuan_jian_store){
            addonBefore_new = form_my_xuan_jian_store.getFieldAddonBefore(field,addonBefore);
            form_my_xuan_jian_store.reload(that);//获取数据后，重新渲染

        }else if(form_store){
            addonBefore_new = form_store.getFieldAddonBefore(field,addonBefore);
            form_store.reload(that);//获取数据后，重新渲染
        }
        return (
            <Form.Item label={label ? label : "输入框"} {...new_props}>
                {getFieldDecorator(field ? field : "default_input", {initialValue: initialValue,rules: rules ? rules : []})(
                    <Input
                        allowClear={allowClear ? allowClear : false}
                        placeholder={placeholder ? placeholder: "请输入..."}
                        {...(disabled ? { disabled: true } : {})}
                        {...(readonly ? { readonly: true } : {})}
                        addonAfter={addonAfter}
                        addonBefore={addonBefore_new}
                        onChange={this.handleInputChange}
                    />
                )}
            </Form.Item>
        );
    }
}

export default FormInput;
